package com.example.hxds.mps.service.impl;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.example.hxds.common.exception.HxdsException;
import com.example.hxds.mps.service.MapService;
import com.google.gson.JsonObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;

@Service
@Slf4j
public class MapServiceImpl implements MapService {

    //预估里程的API地址
    private String distanceUrl = "https://apis.map.qq.com/ws/distance/v1/matrix/";
    //规划行进路线的API地址
    private String direcionUrl="https://apis.map.qq.com/ws/direction/v1/driving/";

    @Value("${tencent.map.key}")
    private String key;

    @Override
    public HashMap estimateOrderMileageAndMinute(String mode, String startPlaceLatitude, String startPlaceLongitude, String endPlaceLatitude, String endPlaceLongitude) {
        HttpRequest request=new HttpRequest(distanceUrl);
        request.form("mode", mode);
        request.form("from",startPlaceLatitude+","+startPlaceLongitude);
        request.form("to",endPlaceLatitude+","+endPlaceLongitude);
        request.form("key",key);
        HttpResponse response = request.execute();
        JSONObject body = JSONUtil.parseObj(response.body());
        Integer status = body.getInt("status");
        String message = body.getStr("message");
        if (status!=0){
            log.error("预估里程异常", message);
            throw new HxdsException("未知系统错误，请重试！"+message);
        }
        JSONArray rows = body.getJSONObject("result").getJSONArray("rows");
        JSONObject element = rows.get(0, JSONObject.class).getJSONArray("elements").get(0, JSONObject.class);

        Integer distance = element.getInt("distance");
        String mileage = new BigDecimal(distance).divide(new BigDecimal(1000)).toString();
        Integer duration = element.getInt("duration");
        String temp = new BigDecimal(duration).divide(new BigDecimal(60), 0, RoundingMode.CEILING).toString();
        int minute = Integer.parseInt(temp);
        return new HashMap(){{
            put("mileage",mileage);put("minute",minute);
        }};
    }
    @Override
    public HashMap calculateDriverLine(String startPlaceLatitude, String startPlaceLongitude, String endPlaceLatitude, String endPlaceLongitude) {
        HttpRequest request = new HttpRequest(direcionUrl);
        request.form("from",startPlaceLatitude+","+startPlaceLongitude);
        request.form("to",endPlaceLatitude+","+endPlaceLongitude);request.form("key",key);
        HttpResponse response = request.execute();
        JSONObject body = JSONUtil.parseObj(response.body());
        Integer status = body.getInt("status");
        String message = body.getStr("message");
        if (status!=0){
            log.error("执行异常", message);
            throw new HxdsException("预估里程异常"+message);
        }
        JSONObject result = body.getJSONObject("result");
        HashMap map = result.toBean(HashMap.class);
        return map;
    }

}
